package app.imttf.online.backing.security;

import app.imttf.online.adfextensions.ADFUtils;
import app.imttf.online.adfextensions.JSFUtils;
import app.imttf.online.backing.admin.user.UserData;
import app.imttf.online.model.SecurityAMImpl;
import app.imttf.online.utils.PasswordEncoder;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;
import oracle.adf.view.rich.component.rich.output.RichMessage;


public class UserPasswordBean {
    private RichInputText newPassword;
    private RichInputText confirmPassword;
    private RichMessage passwordError;
    private RichMessage infoMessage;
    private RichInputText oldPassword;
    private RichInputText userLoginName;
    private RichPanelGroupLayout infoGroupLayout;

    public UserPasswordBean() {
    }

    public void setNewPassword(RichInputText newPassword) {
        this.newPassword = newPassword;
    }

    public RichInputText getNewPassword() {
        return newPassword;
    }

    public void setConfirmPassword(RichInputText confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public RichInputText getConfirmPassword() {
        return confirmPassword;
    }

    public void setPasswordError(RichMessage passwordError) {
        this.passwordError = passwordError;
    }

    public RichMessage getPasswordError() {
        return passwordError;
    }

    public void setInfoMessage(RichMessage infoMessage) {
        this.infoMessage = infoMessage;
    }

    public RichMessage getInfoMessage() {
        return infoMessage;
    }

    public void setOldPassword(RichInputText oldPassword) {
        this.oldPassword = oldPassword;
    }

    public RichInputText getOldPassword() {
        return oldPassword;
    }

    public void setUserLoginName(RichInputText userLoginName) {
        this.userLoginName = userLoginName;
    }

    public RichInputText getUserLoginName() {
        return userLoginName;
    }

    /*
     * Change Password for User Page
     */

    public String changePassword() {
        String action = null;
        String newPass = (String)newPassword.getValue();
        String oldPass = (String)oldPassword.getValue();
        String confPass = (String)confirmPassword.getValue();

        UserData userData =
            (UserData)JSFUtils.resolveExpression("#{UserData}");
        String currentUserName = userData.getUserNameLogin();
        String currentPassword = userData.getPassword();

        String encPassword =
            PasswordEncoder.encodePassword(currentUserName + ":" +
                                           currentPassword);
        String encOldPassword =
            PasswordEncoder.encodePassword(currentUserName + ":" + oldPass);

        if (encPassword.equals(encOldPassword)) {
            if (newPass.equals(confPass)) {
                String encNewPassword =
                    PasswordEncoder.encodePassword(currentUserName + ":" + newPass);
                SecurityAMImpl am =
                    (SecurityAMImpl)ADFUtils.getApplicationModuleForDataControl("SecurityAMDataControl");
                am.changePassword(encNewPassword);
                infoMessage.setRendered(false);
                passwordError.setMessageType("confirmation");
                passwordError.setMessage("Password telah diganti");
                action = null;
            } else {
                infoMessage.setRendered(false);
                passwordError.setMessageType("error");
                passwordError.setMessage("Password baru tidak sama");
                action = null;
            }
        } else {
            infoMessage.setRendered(false);
            passwordError.setMessageType("error");
            passwordError.setMessage("Password lama tidak sesuai");
            action = null;
        }

        return action;
    }

    public void setInfoGroupLayout(RichPanelGroupLayout infoGroupLayout) {
        this.infoGroupLayout = infoGroupLayout;
    }

    public RichPanelGroupLayout getInfoGroupLayout() {
        return infoGroupLayout;
    }
}
